using SharpObjects.EventBus;
using SharpObjects.Shared.ApplicationEvents;

namespace SharpObjects.Shared.Proxies
{
    public class ServiceFactoryProxy
    {
        protected IEventBus eventBus;
   
        protected ServiceFactoryProxy()
        {
            this.eventBus = null;
        }

        public ServiceFactoryProxy(IEventBus eventBus)
        {
            this.eventBus = eventBus;
        }

        public T GetService<T>(string serviceId, string serviceVersion = null)
        {
            ServiceFactoryGetServiceApplicationEvent getServiceApplicationEvent = new ServiceFactoryGetServiceApplicationEvent()
                                                                                      {
                                                                                          ServiceId = serviceId, 
                                                                                          ServiceVersion = serviceVersion
                                                                                      };
            this.eventBus.Publish(getServiceApplicationEvent);

            return getServiceApplicationEvent.GetService<T>();
        }
    }
}